package com.example.serenity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.serenity.controller.model.Response;
import com.example.serenity.dao.CommentMapper;
import com.example.serenity.entity.Comment;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentMapper commentMapper;

    @GetMapping("/list")
    public Response<?> list(@RequestParam("parent") Integer parent){
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent", parent);
        queryWrapper.orderByDesc("id");
        return new Response<>(commentMapper.selectList(queryWrapper));
    }

    @PostMapping("/post")
    public Response<?> list(Comment comment){
        if(comment.getParent()==null||comment.getName()==null||comment.getContent()==null)
            return new Response<>("0","信息不完整！");
        comment.setTime(new Date());
        commentMapper.insert(comment);
        return new Response<>("评论成功啦~");
    }

}
